package com.heima.behavior.service.impl;

import com.heima.behavior.dtos.UnLikesBehaviorDto;
import com.heima.behavior.pojos.ApUnlikesBehavior;
import com.heima.behavior.service.ApUnLikesBehaviorService;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.threadLocal.ApUserThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApUnLikesBehaviorServiceImpl implements ApUnLikesBehaviorService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 存储不喜欢数据
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult unLike(UnLikesBehaviorDto dto) {
        //1.参数效验
        if (dto.getArticleId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"改文章不存在");
        }
        //2.判断是否是登录用户还是游客身份
        ApUser apUser = ApUserThreadLocalUtil.getApUser();
        if (apUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        Integer userId = apUser.getId();
        if (userId==0){
            //3.如果是游客则终止程序
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH,"游客身份无法进行操作");
        }
        //4.判断当前用户是否不喜欢过该文章
        String unLikeKey = "USER_UNLIKES_"+dto.getArticleId();
        Boolean flag = redisTemplate.opsForHash().hasKey(unLikeKey,userId.toString());
        //5.如果不喜欢过,且正在执行的是不喜欢,则终止
        if (flag && dto.getType()==0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"当前用户已经不喜欢过该文章");
        }
        //6.如果不喜欢过,且正在执行的是取消不喜欢,则删除redis和mongo
        if (flag && dto.getType()==1){
            redisTemplate.opsForHash().delete(unLikeKey,userId.toString());
            mongoTemplate.remove(Query.query(Criteria.where("userId").is(userId).and("articleId").is(dto.getArticleId())),ApUnlikesBehavior.class);
        }
        //7.如果没有不喜欢过,且正在执行的是不喜欢,则新增redis和mongo
        if (flag&&dto.getType()==0){
            redisTemplate.opsForHash().put(unLikeKey,userId.toString(),"1");
            ApUnlikesBehavior apUnlikesBehavior  = new ApUnlikesBehavior();
            apUnlikesBehavior.setUserId(Long.valueOf(userId));
            apUnlikesBehavior.setArticleId(dto.getArticleId());
            apUnlikesBehavior.setCreatedTime(new Date());
            mongoTemplate.save(apUnlikesBehavior);
        }
        //8.如果没有不喜欢过,且正在执行的是取消不喜欢,则终止
        if (flag&&dto.getType()==1){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"已经执行过该操作");
        }
        //9.返回数据
        return ResponseResult.okResult("操作成功");
    }
}
